home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / program / inter54e.zip / INTSUM16.ZIP / STRSTRI.ASM < prev    next >
Assembly Source File  |  1996-10-13  |  5KB  |  141 lines

  1. ;*****************************************************************
  2. ;  STRSTRI.ASM - Case-insensitive version of strstr() from        
  3. ;                standard C library.                              
  4. ;                                                                 
  5. ;  Copyright (c) 1996 Daniel D. Miller                            
  6. ;                                                                 
  7. ;  Last Update: 07-23-95 11:36pm                                  
  8. ;*****************************************************************
  9.  
  10.         .model  compact,c
  11.  
  12.         .code
  13.  
  14. ;*****************************************************************
  15. ;  perform case-insensitive compare between two characters,       
  16. ;  return TRUE (nonzero) or FALSE (0) for the compare.            
  17. ;  This routine is used by strstri(), which follows.              
  18. ;*****************************************************************
  19. ; int caseless_equality(const char chr1, const char chr2)
  20. compare_byte proc uses bx s1:byte, s2:byte
  21.         mov     bx,0
  22.         mov     bl,s1
  23.         mov     ah,cs:[char_lookupi+bx]
  24.         mov     bl,s2
  25.         mov     al,cs:[char_lookupi+bx]
  26.         cmp     ah,al
  27.         je      okay
  28.         mov     ax,-1
  29.         ret
  30. okay:
  31.         mov     ax,0
  32.         ret
  33.  
  34. compare_byte endp
  35.  
  36. ;*****************************************************************
  37. ;  Perform case-insensitive search for first occurance of         
  38. ;  str2 within str1.  Returns MATCH (0) or NO_MATCH (0xFFFF).     
  39. ;int strstri(const char* str1, const char *str2)
  40. ;*****************************************************************
  41. strstri proc uses bx ds si es di str1:ptr byte, str2:ptr byte
  42.         local   hdptr:word
  43.  
  44.         lds     si,str1
  45.         les     di,str2
  46.         mov     bl,es:[di]      ; do only ONCE
  47.  
  48. not_matching:
  49.         ;  search for initial match
  50.         mov     bh,ds:[si]
  51.  
  52.         cmp     bh,0
  53.         je      no_match
  54.         cmp     bl,0
  55.         je      no_match
  56.  
  57.         invoke  compare_byte, bh, bl
  58.         cmp     ax,0
  59.         je      matching
  60.         inc     si
  61.         jmp     not_matching
  62.  
  63.         ;  once a single-char match is found,
  64.         ;  come here and continue trying to match later chars
  65. matching:
  66.         mov     hdptr, si
  67. match_loop:
  68.         inc     si
  69.         inc     di
  70.  
  71.         mov     bh,ds:[si]      ; get char from str1
  72.         mov     bl,es:[di]      ; get char from str2
  73.  
  74.         cmp     bl,0
  75.         je      yes_match
  76.         cmp     bh,0
  77.         je      no_match
  78.  
  79.         invoke  compare_byte, bh, bl
  80.         cmp     ax,0
  81.         je      match_loop
  82.  
  83.         ;  match failed, start over
  84.         les     di,str2
  85.         mov     bl,es:[di]      ; get first char from str2
  86.         mov     si,hdptr
  87.         inc     si
  88.         jmp     not_matching
  89.  
  90.         ;  exit points
  91. no_match:
  92.         mov     ax,-1
  93.         ret
  94.  
  95. yes_match:
  96.         mov     ax,0
  97.         ret
  98.  
  99. strstri endp
  100.  
  101. ;*****************************************************************
  102. ;  quick lookup table for character compare.  Every entry is same 
  103. ;  as IBM ASCII, except that lower-case entries (0x60 - 0x7A) are 
  104. ;  same as upper-case entries (0x40 - 0x5A).                      
  105. ;*****************************************************************
  106. ;uchar char_lookupi[256] = {
  107. char_lookupi    db      000h,001h,002h,003h,004h,005h,006h,007h
  108.         db      008h,009h,00Ah,00Bh,00Ch,00Dh,00Eh,00Fh
  109.         db      010h,011h,012h,013h,014h,015h,016h,017h
  110.         db      018h,019h,01Ah,01Bh,01Ch,01Dh,01Eh,01Fh
  111.         db      020h,021h,022h,023h,024h,025h,026h,027h
  112.         db      028h,029h,02Ah,02Bh,02Ch,02Dh,02Eh,02Fh
  113.         db      030h,031h,032h,033h,034h,035h,036h,037h
  114.         db      038h,039h,03Ah,03Bh,03Ch,03Dh,03Eh,03Fh
  115.         db      040h,041h,042h,043h,044h,045h,046h,047h
  116.         db      048h,049h,04Ah,04Bh,04Ch,04Dh,04Eh,04Fh
  117.         db      050h,051h,052h,053h,054h,055h,056h,057h
  118.         db      058h,059h,05Ah,05Bh,05Ch,05Dh,05Eh,05Fh
  119.         db      040h,041h,042h,043h,044h,045h,046h,047h
  120.         db      048h,049h,04Ah,04Bh,04Ch,04Dh,04Eh,04Fh
  121.         db      050h,051h,052h,053h,054h,055h,056h,057h
  122.         db      058h,059h,05Ah,07Bh,07Ch,07Dh,07Eh,07Fh
  123.         db      080h,081h,082h,083h,084h,085h,086h,087h
  124.         db      088h,089h,08Ah,08Bh,08Ch,08Dh,08Eh,08Fh
  125.         db      090h,091h,092h,093h,094h,095h,096h,097h
  126.         db      098h,099h,09Ah,09Bh,09Ch,09Dh,09Eh,09Fh
  127.         db      0A0h,0A1h,0A2h,0A3h,0A4h,0A5h,0A6h,0A7h
  128.         db      0A8h,0A9h,0AAh,0ABh,0ACh,0ADh,0AEh,0AFh
  129.         db      0B0h,0B1h,0B2h,0B3h,0B4h,0B5h,0B6h,0B7h
  130.         db      0B8h,0B9h,0BAh,0BBh,0BCh,0BDh,0BEh,0BFh
  131.         db      0C0h,0C1h,0C2h,0C3h,0C4h,0C5h,0C6h,0C7h
  132.         db      0C8h,0C9h,0CAh,0CBh,0CCh,0CDh,0CEh,0CFh
  133.         db      0D0h,0D1h,0D2h,0D3h,0D4h,0D5h,0D6h,0D7h
  134.         db      0D8h,0D9h,0DAh,0DBh,0DCh,0DDh,0DEh,0DFh
  135.         db      0E0h,0E1h,0E2h,0E3h,0E4h,0E5h,0E6h,0E7h
  136.         db      0E8h,0E9h,0EAh,0EBh,0ECh,0EDh,0EEh,0EFh
  137.         db      0F0h,0F1h,0F2h,0F3h,0F4h,0F5h,0F6h,0F7h
  138.         db      0F8h,0F9h,0FAh,0FBh,0FCh,0FDh,0FEh,0FFh
  139.  
  140.         end
  141.